package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin
public class DictController {

    @Autowired
    DictService dictService;

    /**
     * 导出数据字典
     * @param file
     * @return
     */
    @PostMapping("importData")
    public Result importDict(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }


    /**
     * 根据数据id查询子数据列表
     * @param id
     * @return
     */
    @ApiOperation(value = "根据数据id获得子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> list=dictService.findChidData(id);
        return Result.ok(list);
    }
    /**
     * 导出数据字典接口
     * @param httpServletResponse
     * @return
     */
    @GetMapping("exportData")
    @ApiOperation(value = "导出数据字典")
    public void expoertDict(HttpServletResponse httpServletResponse){
        dictService.exportDictData(httpServletResponse);
    }

    /**
     * 根据dictCode和value查询
     * @return
     */
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName=dictService.getDictName(dictCode,value);
        return dictName;
    }

    /**
     * 根据value查询
     * @return
     */
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        dictService.getDictName("",value);  //前面字段为空，最终调用一个service
        return value;
    }

}
